<template >
  <div class="app-container conserveTask">
    <el-form :model="selectForm" ref="queryForm" :inline="true" label-width="120px">
      <el-form-item label="所属公司：">
        <treeselect v-if="deptOptions" class="option" v-model="selectForm.deptId" :options="deptOptions" placeholder="请选择所属部门" />
      </el-form-item>
      <el-form-item label="任务单号：">
        <el-input size="small"  v-model="selectForm.taskCoder" placeholder="请输入任务单号" clearable/>
      </el-form-item>
      <el-form-item label="计划单号：">
        <el-input size="small"  v-model="selectForm.planCoder" placeholder="请输入计划单号" clearable/>
      </el-form-item>
      <el-form-item label="责任人：">
        <el-input size="small"  v-model="selectForm.chargeUserName" placeholder="请输入责任人" clearable/>
      </el-form-item>
      <el-form-item label="设备名称：">
        <el-input size="small"  v-model="selectForm.deviceName" placeholder="请输入设备名称" clearable/>
      </el-form-item>
      <el-form-item label="计划名称：">
        <el-input size="small"  v-model="selectForm.planName" placeholder="请输入计划名称" clearable/>
      </el-form-item>
      <el-form-item label="任务状态：">
        <el-select size="small"  v-model="selectForm.taskStatusList" placeholder="请选择" clearable>
          <el-option
            v-for="item in taskStatus"
            :key="item.id"
            :label="item.typeName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="截止日期：">
        <el-date-picker
          size="small"
          v-model="selectDate"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="dateChange"
        ></el-date-picker>
      </el-form-item>
      <!-- <el-form-item>
        <el-checkbox v-model="selectForm.checked" >我审核的</el-checkbox>
      </el-form-item> -->
      <el-form-item>
        <el-button size="mini" type="primary" icon="el-icon-search" @click="selectForm.pageNum=1;getList();">搜索</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" ref="singleTable" :data="list" stripe border  >
      <el-table-column align="center" type="index" label="序号" width="55"/>
      <el-table-column align="center" label="任务单号" width="150"  prop="taskCoder">
        <template slot-scope="scope">
          <div class="bule"  @click="details(scope.row)">{{scope.row.taskCoder}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="计划名称" width="150"  prop="planName">
        <template slot-scope="scope">
          <div class="bule"  @click="detailsPlan(scope.row)">{{scope.row.planName}}</div>
        </template>
      </el-table-column>
      <el-table-column align="center" label="设备名称" prop="deviceName"  />
      <el-table-column align="center" label="存放地址" prop="siteName"  />
      <el-table-column align="center" label="责任人" prop="chargeUserName" />
      <el-table-column label="开始日期" align="center" prop="startDate" >
        <template slot-scope="scope">
          <span>{{scope.row.startDate}}</span>
        </template>
      </el-table-column>
      <el-table-column label="截止日期" align="center" prop="endDate" >
        <template slot-scope="scope">
          <span>{{ scope.row.endDate }}</span>
        </template>
      </el-table-column>
      <el-table-column label="执行日期" align="center" prop="executeDate" >
        <template slot-scope="scope">
          <span>{{ scope.row.executeDate }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="任务状态" prop="taskStatus">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.taskStatus==0" type="warning">未执行</el-button>
          <el-button size="mini" v-if="scope.row.taskStatus==1" type="danger">未完成</el-button>
          <el-button size="mini" v-if="scope.row.taskStatus==2" type="success">已完成</el-button>
          <el-button size="mini" v-if="scope.row.taskStatus==3" type="info">已作废</el-button>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center"  fixed="right" width="300">
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.taskStatus==1" type="primary" icon="el-icon-edit" @click="edit(scope.row)">编辑</el-button>
          <el-button size="mini" v-if="scope.row.taskStatus==1||scope.row.taskStatus==0" class="btn-blue" icon="el-icon-refresh-right" @click="redeploy(scope.row)">重新分派</el-button>
          <el-button size="mini" v-if="scope.row.taskStatus==1||scope.row.taskStatus==0" type="danger" icon="el-icon-delete" @click="cancellation(scope.row)">作废</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="total"
      :page.sync="selectForm.pageNum"
      :limit.sync="selectForm.pageSize"
      @pagination="getList"
    />
    <redeploy v-if="redeployDialog" @selectChange="redeployChange" @chooseInfo="chooseInfo"></redeploy>

  </div>
</template>

<script>
  import {treeselect } from "@/api/system/dept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  import redeploy from "../../metrological/metrologicalTask/redeploy";
  import {conservetaskList,conservetaskDel,conservetaskChargeuser} from '@/api/curing/index'
  export default {
    name: "conserveTask.vue",
    components:{
      redeploy,
      Treeselect,
    },
    data() {
      return {
        treedata: [],
        defaultProps: {
          children: "children",
          label: "label"
        },
        deptOptions:'',

        loading: false,//加载
        selectForm: {
          chargeUserName:null,
          departmentId:null,
          deviceName:null,
          endDate:null,
          planCoder:null,
          planName:null,
          startDate:null,
          taskCoder:null,
          taskStatusList:null,
          pageNum: 1,
          pageSize: 10,
          // checked:true,
        },
        list: [],
        taskStatus:[
          {
            id:'0',
            typeName:'未执行',
          },{
            id:'1',
            typeName:'未完成',
          },{
            id:'2',
            typeName:'已完成',
          },{
            id:'3',
            typeName:'已作废',
          }
        ],//任务
        total: 0,
        selectDate: '',//时间
        redeployDialog:false,//转派
        id:null
      }
    },
    created() {
      this.getTreeselect()
      this.getList()
    },
    methods: {
      /** 查询类别下拉树结构 */
      getTreeselect() {
        treeselect().then(response => {
          this.deptOptions = response.data;
        });
      },
      chooseInfo(info){
        conservetaskChargeuser({
          chargeUser:info.userId,
          id:this.id
        }).then(res => {
          if(res.code==200){
            this.redeployDialog=false
            this.$message.success(res.msg);
            this.getList()
          }
        });
      },
      getList() {
        //获取计划列表
        conservetaskList(this.selectForm).then(res => {
          if(res.code==200){
            this.list = res.rows
            this.total = res.total
          }
        });
      },
      //更改时间
      dateChange() {
        this.selectForm.startDate = this.selectDate&&this.selectDate[0]||null
        this.selectForm.endDate = this.selectDate&&this.selectDate[1]||null
      },
      //转派
      redeploy(row){
        this.redeployDialog=true
        this.id=row.id
      },
      //成功回调
      redeployChange(data){
        this.redeployDialog=false
      },
      //作废
      cancellation(row){
        this.$confirm('确定作废吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          conservetaskDel(row.id).then((res)=>{
            if(res.code==200){
              this.$message.success(res.msg);
              this.getList()
            }
          })

        }).catch(() => {});
      },
      edit(row) {
        this.$router.push({path:'/conserve/conserveTaskAdd',query:{id:row.id}});
      },

      details(row){
        this.$router.push({path:'/conserve/conserveTaskDetail',query:{id:row.id}});
      },
      detailsPlan(row){

        this.$router.push({path:'/conserve/conservePlanDetail',query:{id:row.conservePlanId}});
      }
    }
  }
</script>

<style  lang="scss">
  .conserveTask {
    .bule {
      color: #2196F3;
      cursor: pointer;
    }
    .btn-blue{
      background: deepskyblue;
      color: #fff;
    }
    .option{
      width: 200px;
      height:32px;
      .vue-treeselect__control{
        line-height: 32px;
        height:32px !important;
      }
    }
  }
</style>
